package com.study.www.config.protobuf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *  String code = ProtobufIDLGenerator.getIDL(Depts.class);
 * protobuf 的 redis 数据模板
 * @author : Administrator.zhuyanpeng
 * @date : 2019/10/16 - 17:38
 */

// 先去配置文件读取 redis 相关的配置不然会导致  RedisConnectionFactory (LettuceConnectionFactory) 链接为空的
@Component
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Import({RedisAutoConfiguration.class})
@Slf4j
public class ProtobufRedisTemplate extends RedisTemplate<String, Object> {

    public ProtobufRedisTemplate( @Autowired() LettuceConnectionFactory lettuceConnectionFactory) {
        ProtobufRedisSerializer protobufRedisSerializer = new ProtobufRedisSerializer(Object.class);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        setConnectionFactory(lettuceConnectionFactory);
        afterPropertiesSet();
        setKeySerializer(stringRedisSerializer);
        setHashKeySerializer(stringRedisSerializer);
        setValueSerializer(protobufRedisSerializer);
        setHashValueSerializer(protobufRedisSerializer);
        logger.warn("the Lettuce-protobuf starting success,date is -->"+ new Date());
    }
}
